package com.zetyun.tiger.format.date.compact;

import com.zetyun.rt.meta.annotation.ActionMeta;
import com.zetyun.rt.sdk.action.MapAction;
import com.zetyun.rt.sdk.model.RtEvent;
import com.zetyun.rt.sdk.operator.OperatorContext;
import com.zetyun.tiger.format.date.SpecialDateFormatUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;

import static com.zetyun.tiger.format.date.FormatConstant.*;


@ActionMeta(
        id = "tiger.icbus3-date-format",
        tags = {"formatter"},
        category = "map",
        name = "iCbus3DateFormat",
        parameterClass = Ibus3DateFormatParam.class,
        status = ActionMeta.Status.PUBLIC,
        description = "iCBUS系统 表统一日期格式,当 op_ 为 INSERT时，CREATE_TIME 有值，UPDATE_TIME 为null，，此时取 CREATE_TIME；当 op_ 为 UPDATE 时，CREATE_TIME 有值，UPDATE_TIME 有值，此时取UPDATE_TIME"
)
@Slf4j
public class Ibus3DateFormatAction extends MapAction {

    private Map<String, IBus3DataFormatTuple> mapping;

    @Override
    public void init(OperatorContext context) {
        super.init(context);
        Ibus3DateFormatParam parameters = context.getParameters();
        mapping = parameters.toMapping();

        log.info("mapping: {}", mapping);

    }

    @Override
    public RtEvent apply(RtEvent in) throws Exception {

        // 获取目标主题名称
        String sinTopic = in.getHeader(SINK_TOPIC, String.class);

        // 根据目标主题名称寻找日期字段 O(1)
        IBus3DataFormatTuple formatTuple = mapping.get(sinTopic);

        long procTs = System.currentTimeMillis();
        String currentSystemDate = SpecialDateFormatUtil.toYmdHms(procTs);
        in.setField(RT_PROC_TMS, procTs);
        in.setField(RT_PROC_TIME, currentSystemDate);

        String insertDateField = formatTuple.getInsertDateField();
        String updateDateField = formatTuple.getUpdateDateField();


        String opType = in.getField("op_", String.class);
        if (Objects.equals(opType, "UPDATE")) {
            Object dataObject = in.getField(updateDateField);
            if (Objects.nonNull(dataObject) && !dataObject.toString().isEmpty()) {
                Long dataTs = SpecialDateFormatUtil.toTs(dataObject.toString(), true);
                String formatDate = dataObject.toString();
                in.setField(RT_EVENT_TMS, dataTs);
                in.setField(RT_EVENT_TIME, formatDate);
                in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(formatDate));
//                in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(formatDate));
//                in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(formatDate));
            } else {
                in.setField(RT_EVENT_TMS, procTs);
                in.setField(RT_EVENT_TIME, currentSystemDate);
                in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(currentSystemDate));
//                in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(currentSystemDate));
//                in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(currentSystemDate));
            }
        } else if (Objects.equals(opType, "INSERT")) {
            Object dataObject = in.getField(insertDateField);
            if (Objects.nonNull(dataObject) && !dataObject.toString().isEmpty()) {
                Long dataTs = SpecialDateFormatUtil.toTs(dataObject.toString(), true);
                String formatDate = dataObject.toString();
                in.setField(RT_EVENT_TMS, dataTs);
                in.setField(RT_EVENT_TIME, formatDate);
                in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(formatDate));
//                in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(formatDate));
//                in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(formatDate));
            } else {
                in.setField(RT_EVENT_TMS, procTs);
                in.setField(RT_EVENT_TIME, currentSystemDate);
                in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(currentSystemDate));
//                in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(currentSystemDate));
//                in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(currentSystemDate));
            }
        } else {
            in.setField(RT_EVENT_TMS, procTs);
            in.setField(RT_EVENT_TIME, currentSystemDate);
            in.setField(RT_EVENT_DATE, SpecialDateFormatUtil.day(currentSystemDate));
//            in.setField(RT_EVENT_HOUR, SpecialDateFormatUtil.hour(currentSystemDate));
//            in.setField(RT_EVENT_MINU, SpecialDateFormatUtil.minute(currentSystemDate));
        }


        in.setField(RT_EVENT_TYPE, opType);
//        in.setField(RT_EVENT_TYPE, in.getField("op_"));
        in.removeField("op_");
        return in;
    }
}
